package pub.gll.plugin.asm.annotation;

import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

import pub.gll.plugin.Configuration;
import pub.gll.plugin.util.Log;


public class CollectArchBindingDataContainer {
    static final String ARCH_END_POSIX = "_arch_binding";
    private static CollectArchBindingDataContainer container = new CollectArchBindingDataContainer();
    private AtomicInteger realInsertCount = new AtomicInteger(0);
    private HashMap<String, String> bindingClassNameValue = new HashMap<>();
    private Configuration configuration = null;

    /**
     * 收集需要插桩的类
     *
     * @param originalClassName 原始类
     * @param archClassName     原始类生成的以_arch_binding结尾的类
     */
    static synchronized void registerDataClass(String originalClassName, String archClassName) {
        container.bindingClassNameValue.put(originalClassName, archClassName);
    }

    static boolean containsName(String className) {
        return container.bindingClassNameValue.containsKey(className);
    }

    private CollectArchBindingDataContainer() {

    }

    public static int needArchBindSize() {
        return container.bindingClassNameValue.size();
    }

    public static int realArchBindSize() {
        return container.realInsertCount.get();
    }

    public static void increaseRealInsertCount() {
        container.realInsertCount.incrementAndGet();
    }

    public static void reset() {
        container.realInsertCount.set(0);
        container.bindingClassNameValue.clear();
        Log.d("CollectArchBindingDataContainer", "after reset: insertCount = "
                + container.realInsertCount.get()
                + ", bindingClassNameSet =>"
                + container.bindingClassNameValue.size());
    }

    public static  HashMap<String, String> getBindClassNameValue(){
        return container.bindingClassNameValue;
    }
    public static boolean archBindingSizeEquals() {
        return container.realInsertCount.get() == needArchBindSize();
    }

    public static void setConfiguration(Configuration configuration) {
        container.configuration = configuration;
    }

    public static boolean isDebug() {
        return container.configuration != null && container.configuration.debug;
    }
}